<template>
  <div class="container">
    <div class="app-container">
      <!-- 左上角：添加脚色按钮 -->
      <div style="padding: 10px">
        <el-button
          type="primary"
          size="mini"
          @click="showDialog = true"
        >添加角色</el-button>
      </div>
      <!-- 中间：角色列表表格 -->
      <el-table :data="roleList">
        <el-table-column prop="name" label="角色" align="center" width="200">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="启用" align="center" width="200">
          <template #default="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              size="mini"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else size="mini">{{
              row.state === 1 ? "已启用" : row.state === 2 ? "未启用" : "其他"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" size="mini" :rows="2" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <div v-if="row.isEdit">
              <el-button size="mini" type="success" @click="confimEdit(row)">确定</el-button>
              <el-button size="mini" type="danger" @click="row.isEdit = false">取消</el-button>
            </div>
            <div v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" @click="editClick(row)">编辑</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="delEdit(row)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 右下角：角色表格分页 -->
      <el-row style="height: 40px" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager,next"
          :total="pageParams.total"
          :current-page="pageParams.page"
          :page-size="pageParams.pagesize"
          @current-change="currentChange"
        />
      </el-row>
    </div>
    <!-- 弹层组件 -->
    <el-dialog title="新增角色" :visible.sync="showDialog" width="500px">
      <!-- 角色表单 -->
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width: 300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <!-- 如果不需要校验 就不需要写 prop属性 -->
          <el-switch
            v-model="roleForm.state"
            :active-value="1"
            :inactive-value="0"
            size="mini"
          />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input
            v-model="roleForm.description"
            type="textarea"
            :rows="3"
            style="width: 300px"
            size="mini"
          />
        </el-form-item>
        <el-form-item>

          <el-row style="width: 300px" type="flex" justify="center">

            <el-button
              size="mini"
              type="primary"
              @click="confirmClick"
            >确定</el-button>

            <el-button size="mini" @click="cancelClick">取消</el-button>

          </el-row>

        </el-form-item>
      </el-form></el-dialog>
  </div>
</template>
<script>
import { getRolesAPI, addRole, confimEditAPI, delEditAPI } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      showDialog: false,
      roleList: [],
      pageParams: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    return this.getRoles()
  },
  methods: {
    // 获取角色列表信息
    async getRoles() {
      const { rows, total } = await getRolesAPI(this.pageParams)
      this.roleList = rows
      this.pageParams.total = total
      this.roleList.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    currentChange(currentPage) {
      this.pageParams.page = currentPage
      this.getRoles()
    },
    // 点击确定调取添加接口
    confirmClick() {
      this.$refs.roleForm.validate(async(isOk) => {
        if (isOk) {
          await addRole(this.roleForm)
          this.$message.success('添加角色成功')
          this.cancelClick()
          this.getRoles()
        }
      })
    },
    // 点击取消，重置表单，关闭弹框
    cancelClick() {
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 点击编辑
    editClick(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.description = row.description
      row.editRow.state = row.state
    },
    // 确定编辑
    async confimEdit(row) {
      if (row.editRow.name && row.editRow.description) {
        // 调取接口
        await confimEditAPI({ ...row.editRow, id: row.id })
        // 提示用户
        this.$message.success('修改角色成功')
        // 浅拷贝
        Object.assign(row, { ...row.editRow, isEdit: false })
      } else {
        this.$message.error('角色名或描述不能为空')
      }
    },
    async delEdit(row) {
      await delEditAPI(row.id)
      this.$message.success('删除成功')
      if (this.pageParams.page > 1 && this.roleList.length === 1) {
        this.pageParams.page--
      }
      this.getRoles()
    }
  }
}
</script>
